﻿using KPI.Api;
using KPI.Entity;
using KPI.Entity.Entitys.Dto;
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using TYBase;
using WPF_TY_UI;

namespace KPIClient.Views
{

    public partial class ViewQuestionNaireList : TYUserControl
    {
        ViewQuestionNaireListModel vm = new ViewQuestionNaireListModel();
        #region Event_CerateNaire
        public event Action<int> Event_CerateNaire;
        private void FireEvent_CerateNaire(int data)
        {
            Event_CerateNaire?.Invoke(data);
        }
        #endregion

        #region Event_GoNaireRecordUserList
        public event Action<string> Event_GoNaireRecordUserList;
        private void FireEvent_GoNaireRecordUserList(string examId)
        {
            Event_GoNaireRecordUserList?.Invoke(examId);
        }
        #endregion


        #region Event_EditQuestionNaire
        public event Action<string> Event_EditQuestionNaire;
        private void FireEvent_EditQuestionNaire(string data)
        {
            Event_EditQuestionNaire?.Invoke(data);
        }
        #endregion


        public ViewQuestionNaireList()
        {
            InitializeComponent();
            this.DataContext = vm;
            this.Loaded += ViewQuestionNaireList_Loaded;
        }

        private void ViewQuestionNaireList_Loaded(object sender, RoutedEventArgs e)
        {
            BtnClick_Search(null, null);
        }

        private void BtnClick_CreateQuestionNaire(object sender, RoutedEventArgs e)
        {
            FireEvent_CerateNaire(0);
        }

        private async void BtnClick_Search(object sender, RoutedEventArgs e)
        {
            pager.PageIndex = 1;
            await QueryData();
        }

        async Task QueryData()
        {
            vm.ListQuestionNaire.Clear();
            ExamRecordQuery query = new ExamRecordQuery()
            {
                limit = pager.PageSize,
                page = pager.PageIndex,
                examName = vm.Name,
            };
            if (vm.SelectedStatus != null && vm.SelectedStatus.Key >= 0)
            {
                query.status = vm.SelectedStatus.Key;
            }
            if (vm.DtStart != null)
            {
                query.startDate = vm.DtStart;
            }
            if (vm.DtEnd != null)
            {
                query.endDate = vm.DtEnd;
            }


            var rData = await Run(() => QuesionNaireApi.GetQuestionNareList(query));

            if (rData == null)
            {
                MessageBox.Show("数据异常,请检查日志!");
                return;
            }
            if (rData.code != 0)
            {
                MessageBox.Show("数据异常,code:" + rData.code + ":msg:" + rData.msg);
                return;
            }
            if (rData.data != null)
            {
                rData.data.list?.ForEach(x =>
                {
                    QuestionNaireModel model = new QuestionNaireModel();
                    model.FromData(x);
                    model.Index = vm.ListQuestionNaire.Count + 1;
                    vm.ListQuestionNaire.Add(model);
                });
            }
            pager.DataCount = rData.data.total;
            pager.PageCount = rData.data.pages;
        }

        private async void pager_PageIndexChanged(object sender, RoutedEventArgs e)
        {
            await QueryData();
        }

        private void BtnClick_EditQuesionNaire(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as QuestionNaireModel;
            if (dx != null)
            {
                FireEvent_EditQuestionNaire(dx.Data.examId);
            }
        }

        private async void BtnClick_Publish(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as QuestionNaireModel;
            if (dx != null)
            {
                ViewPublishNaire view = new ViewPublishNaire(dx.Data.examId);
                view.ShowDialog();
                await QueryData();
            }
        }

        private async void BtnClick_Delete(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as QuestionNaireModel;
            if (dx != null)
            {
                string id = dx.Data.examId;
                var rData = await Run(() => QuesionNaireApi.DeleteQuestionNaire(id));

                if (rData == null)
                {
                    MessageBox.Show("数据异常,请检查日志!");
                    return;
                }
                if (rData.code != 0)
                {
                    MessageBox.Show("数据异常,code:" + rData.code + ":msg:" + rData.msg);
                    return;
                }
                await QueryData();
                MessageBox.Show(messageBoxText: "已删除!");
            }
        }

        private void BtnClick_ViewUserList(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as QuestionNaireModel;
            if (dx != null)
            {
                FireEvent_GoNaireRecordUserList(dx.Data.examId);
            }
        }
    }
}
